<template>
  <div class="login-container">
    <el-form
      class="login-form"
      :rules="rules"
      ref="form"
      :model="user"
      size="medium"
      @submit.prevent="handleSubmit"
    >
      <!-- <div class="login-form__header">
        <img
          class="login-logo"
          src="@/assets/login_logo.png"
          alt="登录页面"
        >
      </div> -->
      <el-form-item prop="mobile">
        <el-input
          v-model="user.mobile"
          placeholder="请输入手机号"
        >
          <template #prefix>
            <i class="el-input__icon el-icon-user" />
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="pwd">
        <el-input
          v-model="user.pwd"
          type="password"
          placeholder="请输入密码"
        >
          <template #prefix>
            <i class="el-input__icon el-icon-lock" />
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="imgcode">
        <div class="imgcode-wrap">
          <el-input
            v-model="user.imgcode"
            maxlength="4"
            placeholder="请输入验证码"
          >
            <template #prefix>
              <i class="el-input__icon el-icon-key" />
            </template>
          </el-input>
          <img
            class="imgcode"
            alt="验证码"
            :src="captchaSrc"
            @click="loadcaptcha"
          >
        </div>
      </el-form-item>
      <el-form-item>
        <el-button
          class="submit-button"
          type="primary"
          :loading="loading"
          native-type="submit"
        >
          登录
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue'
import { useStore } from '@/store'
import { getCaptcha, login, getUserInfo } from '@/api/common'
import { useRouter, useRoute } from 'vue-router'
import type { IElForm, IFormItemRule } from '@/types/element-plus'

const store = useStore()

const router = useRouter()
const route = useRoute()

const captchaSrc = ref('')
const form = ref<IElForm | null>(null)

const user = reactive({
  mobile: '18272709676',
  pwd: '123123',
  imgcode: '',
  k: '0.197628373947345873'
})

const loading = ref(false)

const rules = ref<IFormItemRule>({
  mobile: [
    { required: true, message: '请输入账号', trigger: 'change' }
  ],
  pwd: [
    { required: true, message: '请输入密码', trigger: 'change' }
  ],
  imgcode: [
    { required: true, message: '请输入验证码', trigger: 'change' }
  ]
})

onMounted(() => {
  loadcaptcha()
})

// 获取验证码
const loadcaptcha = async () => {
  const data = await getCaptcha()
  captchaSrc.value = URL.createObjectURL(data)
}

const loadUserInfo = async () => {
  loading.value = true
  const data = await getUserInfo().finally(() => {
    loading.value = false
  })
  store.commit('setUser', {
    ...data.data
  })
  let redirect = route.query.redirect || '/'
  if (typeof redirect !== 'string') {
    redirect = '/'
  }

  router.replace(redirect)

  // 验证通过 展示 loading
  // 请求提交
  // 处理响应
}

const handleSubmit = async () => {
  // 表单验证
  const valid = await form.value?.validate()
  if (!valid) {
    return false
  }

  loading.value = true
  const data = await login(user).catch(() => {
    loadcaptcha()
    user.imgcode = ''
  }).finally(() => {
    loading.value = false
  })
  if (!data) return
  store.commit('setToken', {
    token: data.data.token
  })
  loadUserInfo()
}

</script>

<style lang="scss" scoped>
.login-container {
  min-width: 400px;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #2d3a4b;
  background-image: url(@/assets/login-bj.jpg);
  background-size: cover;
}
.login-form {
  padding: 30px;
  border-radius: 6px;
  background: #fff;
  min-width: 350px;
  .login-form__header {
    display: flex;
    justify-content: center;
    align-items: center;
    padding-bottom: 30px;
  }

  .el-form-item:last-child {
    margin-bottom: 0;
  }

  .login__form-title {
    display: flex;
    justify-content: center;
    color: #fff;
  }

  .submit-button {
    width: 100%;
  }

  .login-logo {
    width: 271px;
    height: 74px;
  }
  .imgcode-wrap {
    display: flex;
    align-items: center;
    .imgcode {
      height: 37px;
    }
  }
}
</style>
